package com.hxzy.shop.commodity.controller;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.commodity.dto.req.DrugInfoReq;
import com.hxzy.shop.commodity.dto.req.DrugPageReq;
import com.hxzy.shop.commodity.dto.req.DrugUpdateReq;
import com.hxzy.shop.commodity.dto.resp.DrugResp;
import com.hxzy.shop.commodity.service.DrugService;
import com.hxzy.shop.commodity.service.PrescriptiondrugService;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.entity.Prescriptiondrug;
import com.hxzy.shop.utils.WebUtil;
import com.hxzy.shop.vo.Result;
import com.hxzy.shop.vo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author gityyge
 * @Description: 处方药管理
 */

@Slf4j
@RestController
@Api(tags = "处方药管理")
@RequestMapping("/prescription")
public class PrescriptiondrugController {
    /**
     *
     * 2222222
     */

  @Value("${isDev}")
  private Boolean isDev;

  @Resource
  private PrescriptiondrugService prescriptiondrugService;

    @Resource
    private DrugService drugService;


  @ApiOperation("检测是否开通处方药售卖类目")
  @PostMapping("/checkOpenPre")
  public Result<Boolean> checkOpenPre() {
      System.out.println("能否显示呢");
    //获取（刷新）access_token
      String merchantId = WebUtil.getRequest().getHeader(AuthConstant.MERCHANT_ID);
        if (isDev) {
            //测试环境直接放行
            return Result.success(true);
        }

        if (merchantId.equals("1")){
            //测试环境直接放行
            return Result.success(true);
        }

        //先直接放行吧
      return Result.success(true);
  }

    @ApiOperation("新建查询处方药列表(drugIdList为处方药的prescriptionId数组)")
    @PostMapping("/getPrescriptionList")
    public Result<PageInfo<Prescriptiondrug>> getPrescriptionList(@ApiParam(required = true, name = "drugPageReq", value = "处方药分页请求model")
                                                                  @RequestBody DrugPageReq drugPageReq) {

        System.out.println("---getPrescriptionList被调用----");
        PageInfo<Prescriptiondrug> drugPageInfo = prescriptiondrugService.getPrescriptionList(drugPageReq);
        return Result.success(drugPageInfo);
    }

    @ApiOperation("分页查询处方药品")
    @PostMapping("/getDrugPage")
    public Result<PageInfo<DrugResp>> getPreDrugPage(@ApiParam(required = true, name = "drugPageReq", value = "药品分页请求model")
                                                     @RequestBody DrugPageReq drugPageReq) {
        PageInfo<DrugResp> drugPageInfo = prescriptiondrugService.getDrugPage(drugPageReq);
        return Result.success(drugPageInfo);
    }

    @ApiOperation("修改处方药状态")
    @PostMapping("/editState")
    public Result<Boolean> editState(@ApiParam(required = true, name = "addDrugReq", value = "修改处方药状态对象")
                                     @RequestBody DrugUpdateReq state) {
        boolean result = drugService.updateState(state);
        if (result) {
            return Result.success(result);
        } else {
            return Result.failed(ResultCode.FAILED, "修改失败");
        }
    }


    @ApiOperation("查看处方药详情")
    @PostMapping("/showPrescription")
    public Result<DrugResp> showPrescription(@ApiParam(required = true, name = "addDrugReq", value = "商品的id集合")
                                             @RequestBody DrugInfoReq drugInfoReq) {
        DrugResp result = prescriptiondrugService.showPrescription(drugInfoReq);
        return Result.success(result);
    }

    @ApiOperation("删除处方药")
    @PostMapping("/deletePrescription")
    public Result<Boolean> deletePrescription(@ApiParam(required = true, name = "addDrugReq", value = "删除处方药对象")
                                              @RequestBody DrugInfoReq drugInfoReq) {
        boolean result = prescriptiondrugService.deletePrescription(drugInfoReq);
        if (result) {
            return Result.success(result);
        } else {
            return Result.failed(ResultCode.FAILED, "删除失败");
        }
    }

}
